from django.http import JsonResponse
from rest_framework import mixins
from rest_framework.viewsets import ModelViewSet, GenericViewSet

from app_game.serializers import CustomizeSimpleSerializer, UserDefaultSimpleSerializer
from xuanba_model.models import UserDefault, UserCustomize, Users


# def test(request):
#     return JsonResponse({'code': 200, 'data': '测试成功'})


class DefaultDataViewSet(mixins.CreateModelMixin,
                         mixins.UpdateModelMixin,
                         mixins.ListModelMixin,
                         GenericViewSet):
    # todo get 获取用户的页面设置
    # todo post 保存用户页面设置
    queryset = UserDefault.objects.all()
    serializer_class = UserDefaultSimpleSerializer

    def get_queryset(self):
        user = self.request.user
        print(user)
        # AnonymousUser
        if user.id is None and user.id == 1:
            nouser = Users.objects.filter(user_id=1).first()
            return UserDefault.objects.filter(user=nouser, data_type='coin')
        else:
            return UserDefault.objects.filter(user=user, data_type='coin')


class CustomizeViewSet(ModelViewSet):
    # todo get 获取用户自定义的模型 （例如：硬币正面= 爱， 硬币背面= 不爱， 名字 = 爱不爱）
    # todo post 保存用户自定义模型
    # todo put 修改用户自定义模型
    # todo delete 删除用户自定义模型
    queryset = UserCustomize.objects.all()
    serializer_class = CustomizeSimpleSerializer

    def get_queryset(self):
        user = self.request.user
        print(user)
        # AnonymousUser
        if user.id is None and user.id == 1:
            nouser = Users.objects.filter(user_id=1).first()
            return UserCustomize.objects.filter(user=nouser, data_type='coin')
        else:
            return UserCustomize.objects.filter(user=user, data_type='coin')
